package net.xumf.dao.master;

import net.xumf.domain.master.SystemModule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface SystemModuleRepository extends PagingAndSortingRepository<SystemModule, Integer> {

    /**
     * 根据模块id获取模块
     *
     * @param moduleIds 模块id列表
     * @return {@link List<SystemModule>}
     */
    List<SystemModule> findByModuleIdIn(List<Integer> moduleIds);

    List<SystemModule> findByParentId(Integer parentId);

    long countByParentId(Integer parentId);

    @Query(value = "SELECT sm FROM SystemModule sm WHERE sm.parentId = ?1",
            countQuery = "SELECT count(sm) FROM SystemModule sm WHERE sm.parentId = ?1")
    Page<SystemModule> findByParentId(Integer parentId, Pageable pageable);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM SystemModule sm WHERE sm.parentId = ?1 OR sm.moduleId = ?1")
    void deleteByModuleIdOrParentId(Integer moduleId);
}
